package com.xiaodou.mjf.web.console.core.controller;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.xiaodou.mjf.application.core.manager.FinancingManager;
import com.xiaodou.mjf.application.core.service.FinancialPackageService;
import com.xiaodou.mjf.application.core.service.FinancingAttributeService;
import com.xiaodou.mjf.application.core.service.FinancingService;
import com.xiaodou.mjf.application.core.service.FinancingStatisticsService;
import com.xiaodou.mjf.entity.core.FinancialPackage;
import com.xiaodou.mjf.entity.core.Financing;
import com.xiaodou.mjf.infrastructure.common.controller.ApplicationController;
import com.xiaodou.mjf.infrastructure.common.utils.FileUtils;
import com.xiaodou.mjf.infrastructure.common.utils.Global;

/**
 * 小豆盒子Controller
 * 
 * @author BILLY
 *
 */
@Controller
@RequestMapping("financing/box")
public class BoxProductController extends ApplicationController {

	@Resource
	FinancingService financingService;
	@Resource
	FinancingStatisticsService financingStatisticsService;
	@Resource
	FinancingAttributeService financingAttributeService;
	@Resource
	FinancingManager financingManager;
	@Resource
	FinancialPackageService financialPackageService;

	@RequestMapping("")
	public String box(FinancialPackage financialPackage) {

		return "core/finance/box/list";
	}

	@RequestMapping(value = { "data.json" })
	@ResponseBody
	public Map<String, Object> data(FinancialPackage financialPackage) {
		Page<FinancialPackage> page = financialPackageService
				.pageAll(financialPackage);
		return page(page);
	}

	@RequestMapping(value = { "interestRate", "interestRate/{id}" })
	@ResponseBody
	public boolean checkInterestRate(@PathVariable String id, Integer term) {
		boolean flag = false;
		try {
			Financing financing = new Financing();
			financing = financingService.getFinancingByTermAndInvsetStatus(
					term, Financing.INVEST_STATUS_SALE,Financing.TYPE_XIAODOU);
			if (financing == null) {
				flag = true;
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			throw ex;
		}
		return flag;
	}

	@RequestMapping(value = { "form", "form/{id}" }, method = RequestMethod.GET)
	public String form(Model model, @PathVariable String id) {
		try {
			FinancialPackage financialPackage = financialPackageService.get(id);
			model.addAttribute("financialPackage", financialPackage);
			// ftp ip address
			String imgFtpPath = Global.getImgPath();

//			String imgFtpPath = "http://img6.cache.netease.com";
			model.addAttribute("imgFtpPath", imgFtpPath);
//			String[] imgs = financialPackage.getRiskControlImages().split(",");
//			Map<Integer, String> map = new HashMap<Integer, String>();
//			for (int i = 0; i < imgs.length; i++) {
//				map.put(i, imgs[i]);
//			}
//			model.addAttribute("map", map);
			// List<FinancialPackage> lists = financialPackageService.list();
			// model.addAttribute("lists", lists);
		} catch (Exception ex) {
			logger.error(ex.toString());
			throw ex;
		}

		return "core/finance/box/form";
	}

	// @RequestMapping(value = { "save" }, method = RequestMethod.POST)
	// public String save(FinancialPackage financialPackage,
	// @RequestParam("files") MultipartFile[] files, Model model)
	// throws Exception {
	// try {
	// String[] imgs = new String[files.length];
	// // if (files != null && files.length > 0) {
	// // 循环获取file数组中得文件
	// for (int i = 0; i < files.length; i++) {
	// MultipartFile file = files[i];
	// String path = FileUtils.getPath(
	// FinancialPackage.IMAGE_PATH,
	// financialPackage.getId());
	// String fileName = FileUtils.getFileName(file
	// .getOriginalFilename());
	// FileUtils.upload(Global.getFtpHost(), Global.getFtpUser(),
	// Global.getFtpPassword(), Global.getFtpPort(),
	// file.getInputStream(), path, fileName);
	// imgs[i] = path + "/" + fileName;
	// }
	// // }
	// // String imgsPath = "";
	// // for (int i = 0; i < imgs.length; i++) {
	// // if (i == 0) {
	// // imgsPath += imgs[i];
	// // } else {
	// // imgsPath += "," + imgs[i];
	// // }
	// //
	// // }
	// //
	// // if (imgs != null) {
	// // financialPackage.setRiskControlImages(imgsPath);
	// // }
	// financialPackage.setImage1(imgs[0]);
	// financialPackage.setImage2(imgs[1]);
	// financialPackage.setImage3(imgs[2]);
	// // financing.setIcon(path + "/" + fileName);
	// // financingService.save(financing);
	// financialPackageService.save(financialPackage);
	//
	// } catch (Exception ex) {
	// logger.error(ex.toString());
	// ex.printStackTrace();
	// throw ex;
	// }
	// return "redirect:";
	// }

	@RequestMapping(value = { "save" }, method = RequestMethod.POST)
	public String save(FinancialPackage financialPackage, MultipartFile file1,
			MultipartFile file2, MultipartFile file3, Model model)
			throws Exception {
		try {
			String path = FileUtils.getPath(FinancialPackage.IMAGE_PATH,
					financialPackage.getId());
			if(file1.getSize()>0) {
				String fileName1 = FileUtils.getFileName(file1.getOriginalFilename());
				FileUtils.upload(Global.getFtpHost(), Global.getFtpUser(),
						Global.getFtpPassword(), Global.getFtpPort(),
						file1.getInputStream(), path, fileName1);
				financialPackage.setImage1(path + "/" + fileName1);
			}
			if(file2.getSize()>0) {
				String fileName2 = FileUtils.getFileName(file2.getOriginalFilename());
				FileUtils.upload(Global.getFtpHost(), Global.getFtpUser(),
						Global.getFtpPassword(), Global.getFtpPort(),
						file2.getInputStream(), path, fileName2);
				financialPackage.setImage2(path + "/" + fileName2);
			}
			if(file3.getSize()>0) {
				String fileName3 = FileUtils.getFileName(file3.getOriginalFilename());
				FileUtils.upload(Global.getFtpHost(), Global.getFtpUser(),
						Global.getFtpPassword(), Global.getFtpPort(),
						file3.getInputStream(), path, fileName3);
				financialPackage.setImage3(path + "/" + fileName3);
			}
			
			financialPackageService.save(financialPackage);

		} catch (Exception ex) {
			logger.error(ex.toString());
			ex.printStackTrace();
			throw ex;
		}
		return "redirect:";
	}

}
